package com.wukang.photovoltaicinspectionpictures.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.StringRes
import com.wukang.photovoltaicinspectionpictures.R
import com.wukang.photovoltaicinspectionpictures.databinding.DialogCustomBinding
import com.wukang.photovoltaicinspectionpictures.utils.onDebounceClick


/**
 * 通用弹窗
 * @author wukang
 */
class CustomDialog(
    context: Context,
    @StringRes private val titleRes: Int? = null,
    private val titleText: CharSequence? = null,
    @StringRes private val msgRes: Int? = null,
    private val msgText: CharSequence? = null,
    @StringRes private val editNameRes: Int? = null,
    private val editNameText: CharSequence? = null,
    @StringRes private val editContentRes: Int? = null,
    private val editContentText: CharSequence? = null,
    @StringRes private val editHintRes: Int? = null,
    private val editHintText: CharSequence? = null,
    @StringRes private val negativeRes: Int? = null,
    private val negativeText: CharSequence? = null,
    private val negativeListener: (() -> Unit)? = null,
    @StringRes private val positiveRes: Int? = null,
    private val positiveText: CharSequence? = null,
    private val positiveListener: ((String) -> Unit)? = null,
) : Dialog(context, R.style.Primary_Dialog) {
    private val mBinding: DialogCustomBinding = DialogCustomBinding.inflate(LayoutInflater.from(context))

    init {
        setContentView(mBinding.root)
        window?.apply {
            clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
            setAttributes(attributes?.apply {
                width = ViewGroup.LayoutParams.WRAP_CONTENT
                height = ViewGroup.LayoutParams.WRAP_CONTENT
                gravity = Gravity.CENTER
                dimAmount = 0.5f
            })
        }
        setCanceledOnTouchOutside(false)
        setCancelable(false)
    }

    override fun dismiss() {
        currentFocus.also {
            if (it is EditText) {
                context.getSystemService(INPUT_METHOD_SERVICE).apply {
                    if (this is InputMethodManager) {
                        hideSoftInputFromWindow(it.windowToken, 0)
                    }
                }
            }
        }
        // 注意，super放在隐藏软键盘方法下面
        super.dismiss()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding.apply {
            populateTextView(tvTitle, titleRes, titleText)
            populateTextView(tvMsg, msgRes, msgText)
            populateTextView(tvName, editNameRes, editNameText)
            populateTextView(etContent, editContentRes, editContentText, editHintRes, editHintText)
            populateTextView(btCancel, negativeRes, negativeText)
            populateTextView(btConfirm, positiveRes, positiveText)
            btCancel.onDebounceClick {
                cancel()
                negativeListener?.invoke()
            }
            btConfirm.onDebounceClick {
                cancel()
                positiveListener?.invoke(etContent.text.toString())
            }
            ivClose.onDebounceClick { cancel() }
        }
    }

    private fun populateTextView(view: TextView, res: Int?, text: CharSequence?, hintRes: Int? = null, hintText: CharSequence? = null) {
        if (res != null && res != 0) {
            view.visibility = View.VISIBLE
            view.setText(res)
            populateViewHint(view, hintRes, hintText)
            return
        }
        if (!text.isNullOrEmpty()) {
            view.visibility = View.VISIBLE
            view.text = text
            populateViewHint(view, hintRes, hintText)
            return
        }
        view.visibility = View.GONE
        return
    }

    private fun populateViewHint(view: TextView, hintRes: Int?, hintText: CharSequence?) {
        if (hintRes != null && hintRes != 0) {
            view.setHint(hintRes)
            return
        }
        if (!hintText.isNullOrEmpty()) {
            view.hint = hintText
        }
    }
}